{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## [snake](https://www.30secondsofcode.org/python/s/snake)\n",
    "Python, String, Regexp, Intermediate\n",
    "\n",
    "Converts a string to snake case.\n",
    "\n",
    "Break the string into words and combine them adding `_` as a separator, using a regexp.\n",
    "\n",
    "[Python中常用的处理数据的方法——re.sub()函数 （正则表达式方面的函数）](https://blog.csdn.net/qq_33472765/article/details/80838317)\n",
    "\n",
    "`re.sub`的含义，作用，功能就是：\n",
    "\n",
    "**对于输入的一个字符串，利用正则表达式（的强大的字符串处理功能），去实现（相对复杂的）字符串替换处理，然后返回被替换后的字符串**\n",
    "\n",
    "`re.sub`共有五个参数：\n",
    "- 其中三个必选参数：pattern, repl, string\n",
    "- 两个可选参数：count, flags\n",
    "\n",
    "> pattern，表示正则中的模式字符串。\n",
    "\n",
    ">repl，就是replacement，被替换，的字符串的意思。  \n",
    ">repl可以是字符串，也可以是函数。\n",
    "\n",
    ">string，即表示要被处理，要被替换的那个string字符串。\n",
    "\n",
    ">匹配到的内容，只处理其中一部分。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-27T15:41:30.072862Z",
     "start_time": "2020-08-27T15:41:30.067767Z"
    }
   },
   "outputs": [],
   "source": [
    "from re import sub\n",
    "\n",
    "def snake(s):\n",
    "    return '_'.join(\n",
    "        sub('([A-Z][a-z]+)', r' \\1',\n",
    "            sub('([A-Z]+)', r' \\1',s.replace('-', ' '))\n",
    "        ).split()\n",
    "    ).lower()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-27T15:42:31.249350Z",
     "start_time": "2020-08-27T15:42:31.242656Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "camel_case\n",
      "some_text\n",
      "some_mixed_string_with_spaces_underscores_and_hyphens\n",
      "all_the_small_things\n"
     ]
    }
   ],
   "source": [
    "print(snake('camelCase'))\n",
    "print(snake('some text'))\n",
    "print(snake('some-mixed_string With spaces_underscores-and-hyphens'))\n",
    "print(snake('AllThe-small Things'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## [kebab](https://www.30secondsofcode.org/python/s/kebab)\n",
    "Python, String, Regexp, Intermediate\n",
    "\n",
    "Converts a string to kebab case.\n",
    "\n",
    "Break the string into words and combine them adding `-` as a separator, using a regexp."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-28T07:10:08.470865Z",
     "start_time": "2020-08-28T07:10:08.465410Z"
    }
   },
   "outputs": [],
   "source": [
    "from re import sub\n",
    "\n",
    "def kebab(s):\n",
    "    return '-'.join(\n",
    "        sub(r\"(\\s|_|-)+\",\" \",\n",
    "        sub(r\"[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+\",\n",
    "            lambda mo: ' ' + mo.group(0).lower(), s)\n",
    "        ).split()\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-28T07:10:39.669653Z",
     "start_time": "2020-08-28T07:10:39.660615Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "camel-case\n",
      "some-text\n",
      "some-mixed-string-with-spaces-underscores-and-hyphens\n",
      "all-the-small-things\n"
     ]
    }
   ],
   "source": [
    "print(kebab('camelCase'))\n",
    "print(kebab('some text'))\n",
    "print(kebab('some-mixed_string With spaces_underscores-and-hyphens'))\n",
    "print(kebab('AllThe-small Things'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## [camel](https://www.30secondsofcode.org/python/s/camel)\n",
    "Python, String, Regexp, Intermediate\n",
    "\n",
    "Converts a string to camelcase.\n",
    "\n",
    "Use `re.sub()` to replace any `-` or `_` with a space, using the regexp `r\"(_|-)+\"`. Use `title()` to capitalize the first letter of each word convert the rest to lowercase. Finally, use `replace()` to remove spaces between words."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-28T07:17:27.285545Z",
     "start_time": "2020-08-28T07:17:27.280062Z"
    }
   },
   "outputs": [],
   "source": [
    "from re import sub\n",
    "\n",
    "def camel(s):\n",
    "    s = sub(r\"(_|-)+\", \" \", s).title().replace(\" \", \"\")\n",
    "    return s[0].lower() + s[1:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-28T07:18:47.595043Z",
     "start_time": "2020-08-28T07:18:47.586772Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "someDatabaseFieldName\n",
      "someLabelThatNeedsToBeCamelized\n",
      "someJavascriptProperty\n",
      "someMixedStringWithSpacesUnderscoresAndHyphens\n"
     ]
    }
   ],
   "source": [
    "print(camel('some_database_field_name'))\n",
    "print(camel('Some label that needs to be camelized'))\n",
    "print(camel('some-javascript-property'))\n",
    "print(camel('some-mixed_string with spaces_underscores-and-hyphens'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## [split](https://www.30secondsofcode.org/python/s/split-lines)\n",
    "Python, String, Beginner\n",
    "\n",
    "Splits a multiline string into a list of lines.\n",
    "\n",
    "Use `s.split()` and `'\\n'` to match line breaks and create a list.\n",
    "\n",
    "`str.splitlines()` provides similar functionality to this snippet."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-28T07:14:32.596771Z",
     "start_time": "2020-08-28T07:14:32.588940Z"
    },
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['This', 'is a', 'multiline', 'string.', '']\n",
      "['hello,', 'this', 'is', 'my', 'notebook', 'code', 'repositorie.']\n"
     ]
    }
   ],
   "source": [
    "print('This\\nis a\\nmultiline\\nstring.\\n'.split(\"\\n\"))\n",
    "print(\"hello, this is my notebook code repositorie.\".split(\" \"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## [is_anagram](https://www.30secondsofcode.org/python/s/is-anagram)\n",
    "Python, String, Intermediate\n",
    "\n",
    "Checks if a string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters).\n",
    "\n",
    "Use `isalnum()` to filter out non-alphanumeric characters, `lower()` to transform each character to lowercase. Use `collections.Counter` to count the resulting characters for each string and compare the results.\n",
    "\n",
    "`Python isalnum()` 方法检测字符串是否由字母和数字组成。\n",
    "\n",
    "乱序的字谜，可以填充其它字符（非数字、字母）；也可以理解为统计两个字符串含有的各个字符数量是否相等。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-28T07:22:58.684529Z",
     "start_time": "2020-08-28T07:22:58.678539Z"
    }
   },
   "outputs": [],
   "source": [
    "from collections import Counter\n",
    "\n",
    "def is_anagram(s1, s2):\n",
    "    return Counter(\n",
    "        c.lower() for c in s1 if c.isalnum()\n",
    "    ) == Counter(\n",
    "        c.lower() for c in s2 if c.isalnum()\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-28T07:26:12.697777Z",
     "start_time": "2020-08-28T07:26:12.691601Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "is_anagram(\"#abnagram\", \"bNag a ram@#$%\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## n_times_string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-28T07:30:57.568922Z",
     "start_time": "2020-08-28T07:30:57.562056Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "anlzou anlzou anlzou anlzou anlzou \n"
     ]
    }
   ],
   "source": [
    "print(\"anlzou \"*5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## reverse_string"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-28T07:32:04.087635Z",
     "start_time": "2020-08-28T07:32:04.080298Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "teppins\n"
     ]
    }
   ],
   "source": [
    "text = \"snippet\"\n",
    "print(text[::-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## [capitalize_every_word](https://www.30secondsofcode.org/python/s/capitalize-every-word)\n",
    "Python, String, Beginner\n",
    "\n",
    "Capitalizes the first letter of every word in a string.\n",
    "\n",
    "Use `s.title()` to capitalize first letter of every word in the string."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-28T08:06:11.213743Z",
     "start_time": "2020-08-28T08:06:11.209474Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello World\n",
      "Hi, Anlzou. Nice To Meet You.\n"
     ]
    }
   ],
   "source": [
    "print(\"hello world\".title())\n",
    "print(\"Hi, anlzou. nice to meet you.\".title())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## [decapitalize](https://www.30secondsofcode.org/python/s/decapitalize) & capitalize\n",
    "Python, String, Intermediate\n",
    "\n",
    "Decapitalizes the first letter of a string.\n",
    "\n",
    "Decapitalize the first letter of the string and then add it with rest of the string. Omit the `upper_rest` parameter to keep the rest of the string intact, or set it to `True` to convert to uppercase.\n",
    "\n",
    "1. 开头小写，根据第二个参数，Ture、[1:]的字符大写；False、[1:]的字符不改变\n",
    "\n",
    "2. 开头大写，根据第二个参数，Ture、[1:]的字符小写；False、[1:]的字符不改变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-28T07:43:41.993262Z",
     "start_time": "2020-08-28T07:43:41.985357Z"
    }
   },
   "outputs": [],
   "source": [
    "# 1\n",
    "def decapitalize(s, upper_rest=False):\n",
    "    return s[:1].lower() + (s[1:].upper() if upper_rest else s[1:])\n",
    "\n",
    "# 2\n",
    "def capitalize(s, lower_rest=False):\n",
    "    return s[:1].upper() + (s[1:].lower() if lower_rest else s[1:])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-28T07:43:43.739789Z",
     "start_time": "2020-08-28T07:43:43.733241Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fooBar\n",
      "fOOBAR\n",
      "FooBar\n",
      "Foobar\n"
     ]
    }
   ],
   "source": [
    "print(decapitalize('FooBar'))\n",
    "print(decapitalize('FooBar', True))\n",
    "\n",
    "print(capitalize('FooBar'))\n",
    "print(capitalize('FooBar', True))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  [palindrome](https://www.30secondsofcode.org/python/s/palindrome)\n",
    "Python, String, Intermediate\n",
    "\n",
    "Returns `True` if the given string is a palindrome, `False` otherwise.\n",
    "\n",
    "Use `s.lower()` and `re.sub()` to convert to lowercase and remove non-alphanumeric characters from the given string. Then, compare the new string with its reverse.\n",
    "\n",
    "`\\W`：匹配非字母、数字、下划线。等价于 `[^A-Za-z0-9_]`。\n",
    "\n",
    "去掉指定字符的回文串判断"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-28T07:58:52.011910Z",
     "start_time": "2020-08-28T07:58:52.006385Z"
    }
   },
   "outputs": [],
   "source": [
    "from re import sub\n",
    "\n",
    "def palindrome(s):\n",
    "    s = sub('[\\W_]', '', s.lower())\n",
    "    print(s)\n",
    "    return s == s[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-28T07:59:24.615768Z",
     "start_time": "2020-08-28T07:59:24.609270Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tacocat\n",
      "True\n",
      "hah1a1hah\n",
      "True\n"
     ]
    }
   ],
   "source": [
    "print(palindrome('taco cat'))\n",
    "print(palindrome('Ha__h1a1_hah'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## [byte_size](https://www.30secondsofcode.org/python/s/byte-size)\n",
    "Python, String, beginner\n",
    "\n",
    "Returns the length of a string in bytes.\n",
    "\n",
    "Use `s.encode('utf-8')` to encode the given string and return its length.## "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-28T08:02:45.738821Z",
     "start_time": "2020-08-28T08:02:45.733099Z"
    }
   },
   "outputs": [],
   "source": [
    "def byte_size(s):\n",
    "    a = s.encode('utf-8')\n",
    "    print(a)\n",
    "    return len(s.encode('utf-8'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2020-08-28T08:02:47.745738Z",
     "start_time": "2020-08-28T08:02:47.739714Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'\\xf0\\x9f\\x98\\x80'\n",
      "4\n",
      "b'Hello World'\n",
      "11\n"
     ]
    }
   ],
   "source": [
    "print(byte_size('😀'))\n",
    "print(byte_size('Hello World'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": false
  },
  "varInspector": {
   "cols": {
    "lenName": 16,
    "lenType": 16,
    "lenVar": 40
   },
   "kernels_config": {
    "python": {
     "delete_cmd_postfix": "",
     "delete_cmd_prefix": "del ",
     "library": "var_list.py",
     "varRefreshCmd": "print(var_dic_list())"
    },
    "r": {
     "delete_cmd_postfix": ") ",
     "delete_cmd_prefix": "rm(",
     "library": "var_list.r",
     "varRefreshCmd": "cat(var_dic_list()) "
    }
   },
   "types_to_exclude": [
    "module",
    "function",
    "builtin_function_or_method",
    "instance",
    "_Feature"
   ],
   "window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
